package Handlers;

import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTextArea;

import ClientSide.Client;

/**
 * @author Avishai Kopeliansky 
 * @ID 066717752
 * @author David Shimshon
 * @ID 038185617
 */
public class ClientHandler extends AbsHandler {
	
	private Client c;
	/**
	 * Handles the message
	 * @param header Header of the String - Type of message
	 * @param body	 Body of the message
	 * @param pw 	 PrintWriter
	 * @param jta	 MessageBoard to append text to 
	 * @param list 	 List of clients
	 */
	public void sorter(String header,String body, PrintWriter pw, JTextArea jta,JList<String> list)
	{
		Stater(header);
		switch(_STAT)
		{
		case 0:
			UMSender(body,jta);
			break;
		case 1:
			PMSender(body,jta);
			break;
		case 2:
			SMSender(body,jta);
			break;
		case -1:
			COSender(body,pw,jta);
			break;
		case 3:
			LSender(body, list);
			break;

		}
	}
	/**
	 * Client joins the handler in case he needs to be d/c
	 * @param c CLient to join 
	 */
	public void join(Client c)
	{
		this.c = c;
	}
	protected void PMSender(String body, JTextArea jta){
		jta.append(body+"\n");
	}
	protected void UMSender(String body, JTextArea jta){
		jta.append(body+"\n");
	}
	protected void SMSender(String body,JTextArea jta){
		if(body.contains("Newnick:"))
		{
			int i = body.indexOf(':');
			c.setNames(body.substring(i+1));		
		jta.append("Nick has been changed\n");
		}
		else
		{
			jta.append(body +"\n");
		}
	}
	//if connection is approved send a request for user list
	protected void COSender(String body, PrintWriter pw, JTextArea jta){
		jta.append("connected! \n");
		pw.println("UserList");
	}	
	//Get the user list from the server and update the List in the client window.
	protected void LSender(String body, JList<String> list){
		StringTokenizer st1 = new StringTokenizer(body,",");							
		DefaultListModel<String> dlm = new DefaultListModel<String>();							
		while(st1.hasMoreElements())
		{
			dlm.addElement(st1.nextToken());
		}
		list.setModel(dlm);
	}	


	protected void Stater(String header){
		if(header.equalsIgnoreCase("msg"))
		{
			_STAT = _PMSG;
		}
		else if(header.equalsIgnoreCase("umsg")) 
		{
			_STAT = _UMSG;
		}
		else if(header.equalsIgnoreCase("smsg")) 
		{
			_STAT = _SMSG;
		}
		else if(header.equalsIgnoreCase("connected")) 
		{
			_STAT = -1;
		}

		else 
		{
			_STAT = _ULIST;
		}
	}
	
	/**
	 * Personal message builder
	 * @param name name of recipient
	 * @param sender name of sender
	 * @param body 	body of the messsage
	 * @return string "<msg><"+name+"><"+sender+":"+body+">"
	 */
	public static String getPM(String name, String sender, String body)
	{
		String t = "<msg><"+name+"><"+sender+":"+body+">";
		return t;
	}
	
	/**
	 * Universal message builder
	 * @param sender name of sender
	 * @param body	body of the message
	 * @return String "<umsg><"+sender+":"+body+">"
	 */
	public static String getUM(String sender, String body)
	{
		String t = "<umsg><"+sender+":"+body+">";
		return t;
	}
	
	/**
	 * Auth Command getter
	 * @param name Name to auth
	 * @return String "<Auth><"+name+">"
	 */
	public static String auth(String name){
		String t = "<Auth><"+name+">";
		return t;
		
	}


}
